package com.nulldev.util.security.encryptAPI.impls.experimental.shifter.misc;

public class RabinKarpHash {

	public RabinKarpHash(int myn) {
		n = myn;
		BtoN = 1;
		for (int i = 0; i < n; ++i) {
			BtoN *= B;
		}
	}

	public int eat(char c) {
		hashvalue = B * hashvalue + hasher.hashvalues[c];
		return hashvalue;
	}

	public int update(char outchar, char inchar) {
		hashvalue = B * hashvalue + hasher.hashvalues[inchar] - BtoN * hasher.hashvalues[outchar];
		return hashvalue;
	}

	public static int nonRollingHash(CharSequence s) {
		int value = 0;
		for (int i = 0; i < s.length(); ++i) {
			char c = s.charAt(i);
			int z = hasher.hashvalues[c];
			value = B * value + z;
		}
		return value;
	}

	public int hashvalue;
	int n;
	int BtoN;
	static CharacterHash hasher = CharacterHash.getInstance();
	final static int B = 31;

}